Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

OnBefore Script for transform map help

Alon Grod
Tera Expert

Hi,

Im trying to create OnBefore script:

Email - Exists?
IF Not exists, create record  in customer_contact table.

IF Email Exists:
Check if (Account == Company)
Just update the record in the customer_contact table.

else:
go to customer_contact relationship and company and contact from the sheet.

 

What am I doing wrong? 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    var bs = new GlideRecord('customer_contact');
    bs.addEncodedQuery('email=' + source.u_email);
    bs.query();
    if (!bs.next()) {

        var customer_contact = new GlideRecord('customer_contact');
        customer_contact.initialize();
        customer_contact.setDisplayValue('account', source.u_account);
        customer_contact.setValue('first_name', source.u_first_name);
        customer_contact.setValue('last_name', source.u_last_name);
        customer_contact.setValue('user_name', source.u_email);
        customer_contact.setDisplayValue('location', source.u_location__ci_tate__country_);
        customer_contact.setValue('email', source.u_email);
        customer_contact.setValue('country', source.u_country_code);
        customer_contact.setValue('u_infra_user', source.u_infra_user);
        customer_contact.setValue('phone', source.u_business_phone);
        customer_contact.insert();

    } else {

        if (account == source.u_account) {

            bs.setValue('first_name', source.u_first_name);
            bs.setValue('last_name', source.u_last_name);
            bs.setDisplayValue('location', source.u_location__ci_tate__country_);
            bs.setValue('country', source.u_country_code);
            bs.setValue('u_infra_user', source.u_infra_user);
            bs.setValue('phone', source.u_business_phone);
            bs.update();

        }
        else{
        var sn_customer_relationship = new GlideRecord('sn_customer_relationship');
    sn_customer_relationship.setDisplayValue('company', source.u_account);
    sn_customer_relationship.setValue('contact', bs.getUniqueValue());
    sn_customer_relationship.insert();
            current.setAbortAction(true);
        }
     
    }


})(source, map, log, target);

5 REPLIES 5

Hello,

Could you please share the screenshot of your transform map form, field mapping and the latest onBefore transform script

Thanks