The CreatorCon Call for Content is officially open! Get started here.

Fix script.

Thalavai m
Tera Contributor

Hi all,

I want to copy records from a field in child table to a field in parent table I have tried fix script for this but its not copied can you please help me to do this. and this is the script I have tried 

(function executeRule(current) {

   var parentTable = 'cmdb_ci_service_auto';

   var childTable = 'cmdb_ci_service_discovered';

   

   var parentRecord = new GlideRecord(parentTable);

   if (parentRecord.get(current.parent)) {

       var childRecord = new GlideRecord(childTable);

       if (childRecord.get(current.child)) {

           parentRecord.u_ug_it_service_owner = childRecord.u_it_service_owner;

           parentRecord.update();

       }

   }

})(current);

7 REPLIES 7

Maddysunil
Kilo Sage

@Thalavai m 

     Please verify below points and i have applied some logs to inspect:

  1. Correct Field Names: Ensure that the field names (u_ug_it_service_owner) exist on both the parent (cmdb_ci_service_auto) and child (cmdb_ci_service_discovered) tables.

  2. Check Parent and Child References: Verify that the references (current.parent and current.child) are correctly retrieving the parent and child record IDs. Make sure current.parent and current.child actually contain the sys_ids of the parent and child records respectively.

 

(function executeRule(current) {
    var parentTable = 'cmdb_ci_service_auto';
    var childTable = 'cmdb_ci_service_discovered';

    var parentRecord = new GlideRecord(parentTable);
    if (parentRecord.get(current.parent)) {
        gs.info('Parent record found: ' + parentRecord.getDisplayValue());
        
        var childRecord = new GlideRecord(childTable);
        if (childRecord.get(current.child)) {
            gs.info('Child record found: ' + childRecord.getDisplayValue());
            
            parentRecord.u_ug_it_service_owner = childRecord.u_it_service_owner;
            parentRecord.update();
            
            gs.info('Field value copied successfully.');
        } else {
            gs.info('Child record not found for sys_id: ' + current.child);
        }
    } else {
        gs.info('Parent record not found for sys_id: ' + current.parent);
    }
})(current);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

Hi Maddysunil

I tried your script but its showing 

Thalavaim_0-1712757026707.png

 

@Thalavai m 

You can not use current in the background script and if you are using in fix script then you have to use the object of current table record. Directly current object you can not use.

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Subhashis Ratna
Tera Guru

HI @Thalavai m 

I have the same requirement and updated your code . However, you can try implementing this logic.

var parentTable = 'cmdb_ci_service_auto';
var childTable = 'cmdb_ci_service_discovered';

var parentRecord = new GlideRecord(parentTable);
var childRecord = new GlideRecord(childTable);

if (parentRecord.get(current.parent) && childRecord.get(current.child)) {
    parentRecord.u_ug_it_service_owner = childRecord.u_it_service_owner;
    parentRecord.setWorkflow(false);
    parentRecord.autoSysFields(false);
    parentRecord.update();
    gs.info("Field copied successfully from child to parent.");
} else {
    if (!parentRecord.isValidRecord())
        gs.info("Parent record not found.");
    if (!childRecord.isValidRecord())
        gs.info("Child record not found.");

SS - 1 
Here i have updated as per your requirement .

SubhashisRatna_0-1712746451510.png

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

Thanks,
Subhashis Ratna