Create parent customer from email domain.

Create parent(master) customer from email domain.

function createParent(company, emailId) {
            let companyRecord = record.create({
                type: record.Type.CUSTOMER,
                isDynamic: true,
            });
            companyRecord.setText({
                fieldId: "companyname",
                text: company
            })
            companyRecord.setValue({
                fieldId: "email",
                value: emailId
            })
            companyRecord.setValue({
                fieldId: "custentity_jj_auto_create_sub_customer",
                value: true
            })
            let recordId = companyRecord.save({
                enableSourcing: false,
                ignoreMandatoryFields: false
            });
        }

const beforeSubmit = (scriptContext) => {
           let customerRecord = scriptContext.newRecord;
           let emailId = customerRecord.getValue({
               fieldId: "email"
           })
           let email = emailId;
           let splitTerm = email.split('@');
           let domainName = splitTerm[1];
           let isIndividual = customerRecord.getValue({
               fieldId: "isperson"
           })
           let companyName = domainName.split('.');
           let newCompanyName = companyName[0];
           if (isIndividual == "F") {
              let companyParent = createParent("Parent:" + newCompanyName, emailId)

                  customerRecord.setText({
                       fieldId:"parent",
                       text:"Parent:" + newCompanyName
                  });
           }
           else if (isIndividual == "T") {
               createParent(newCompanyName, emailId);

               customerRecord.setText({
                      fieldId:"parent",
                      text:newCompanyName
               });
          }
}     

Leave a comment

Your email address will not be published. Required fields are marked *