Catalog item - business rule

Alex-Lima84
Tera Contributor

Hello.

 

I'm trying to save my catalog variables in my custom table fields with an After Business Rule. Could anyone tell my why this isn't working? Or should I script it differently. I want to save them after I submit my catalog item.

(function executeRule(current, previous /*null when async*/ ) {
    try {
        var variables = current.variables;

        var customTable = new GlideRecord('x_976040_app_pro_job_application_process');
        customTable.initialize();

        customTable.ritm_number = current.number;
        customTable.full_name = variables.app_full_name;
        customTable.zip_code = variables.app_zip_code;
        customTable.address = variables.app_address;
        customTable.address_number = variables.app_address_number;
        customTable.neighborhood = variables.app_address_neighborhood;
        customTable.city = variables.app_address_city;
        customTable.candidate_state = variables.app_address_state;
        customTable.e_mail = variables.app_email;
        customTable.contact_number = variables.app_number;
        customTable.introduction_info = variables.app_introduction_text;
        customTable.resume = variables.app_upload_resume;

        customTable.insert();
    } catch (ex) {

        gs.error('An error occurred in the business rule: ' + ex);
        throw ex;
    }
})(current, previous);
1 ACCEPTED SOLUTION

Make the business rule an After business rule and check if a record already exists for the RITM

(function executeRule(current, previous /*null when async*/ ) {
    try {
        var variables = current.variables;

        var customTable = new GlideRecord('x_976040_app_pro_job_application_process');
        customTable.addQuery('ritm_number',current.number); // if using sys_id, replace number with sys_id
        customTable.query();

       if (!customTable.next())
       {
        customTable.initialize();

        customTable.ritm_number = current.number;
        customTable.full_name = variables.app_full_name;
        customTable.zip_code = variables.app_zip_code;
        customTable.address = variables.app_address;
        customTable.address_number = variables.app_address_number;
        customTable.neighborhood = variables.app_address_neighborhood;
        customTable.city = variables.app_address_city;
        customTable.candidate_state = variables.app_address_state;
        customTable.e_mail = variables.app_email;
        customTable.contact_number = variables.app_number;
        customTable.introduction_info = variables.app_introduction_text;
        customTable.resume = variables.app_upload_resume;

        customTable.insert();
}
    } catch (ex) {

        gs.error('An error occurred in the business rule: ' + ex);
        throw ex;
    }
})(current, previous);

 


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

Your business rule should be on sc_req_item table instead of sc_cat_item table.

Also of variables.app_full_name, can you try current.variables.app_full_name;

And ritm_number should be a reference field and you should store the sysid of the RITM, so that you can easily navigate to the RITM from the custom table.


Please mark this response as correct or helpful if it assisted you with your question.

Alex-Lima84
Tera Contributor

Thank you @SanjivMeher 

 

It worked. But when I click submit in my catalog item, it creates 7 repeated records in my custom table. Is there a way to prevent this? Maybe there is something that I need to change in my function.

Make the business rule an After business rule and check if a record already exists for the RITM

(function executeRule(current, previous /*null when async*/ ) {
    try {
        var variables = current.variables;

        var customTable = new GlideRecord('x_976040_app_pro_job_application_process');
        customTable.addQuery('ritm_number',current.number); // if using sys_id, replace number with sys_id
        customTable.query();

       if (!customTable.next())
       {
        customTable.initialize();

        customTable.ritm_number = current.number;
        customTable.full_name = variables.app_full_name;
        customTable.zip_code = variables.app_zip_code;
        customTable.address = variables.app_address;
        customTable.address_number = variables.app_address_number;
        customTable.neighborhood = variables.app_address_neighborhood;
        customTable.city = variables.app_address_city;
        customTable.candidate_state = variables.app_address_state;
        customTable.e_mail = variables.app_email;
        customTable.contact_number = variables.app_number;
        customTable.introduction_info = variables.app_introduction_text;
        customTable.resume = variables.app_upload_resume;

        customTable.insert();
}
    } catch (ex) {

        gs.error('An error occurred in the business rule: ' + ex);
        throw ex;
    }
})(current, previous);

 


Please mark this response as correct or helpful if it assisted you with your question.

Thank you @SanjivMeher 

 

I tried your solution and the problem persists. But now, instead of 7 repeated records, it creates 6.