Salesforce Sales order trigger for CRM systems

Source: Internet
Author: User
Tags new set
Trigger Salesordertrigger on Salesorder__c (before Insert,before update,after insert) {Salesorder__c SalesOrder = Tri
    GGER.NEW[0];
    Account A = new account (); if (Trigger.isinsert&&trigger.isbefore) {if (salesorder.customer__c!=null) {a = [SELECT id,te Chnologicalrequirements1__c,customer_status__c, industry, Material_type2__c, Name, Phone__c, Deliveryaddress__c from
            Account WHERE Id =: Salesorder.customer__c]; list<contact> contactlist = [select Id,name,firstname,lastname,mobilephone,phone_number2__c from Contact where
            Accountid=:salesorder.customer__c Limit 1]; If (' not enabled ') equals (A.customer_status__c) &&contactlist.size () <=0) {Salesorder.adderror (' customer has no relevant contact, is not enabled, please maintain and submit approvals in a timely manner.
            '); }else{if (' not enabled '. Equals (A.customer_status__c)) {Salesorder.adderror (' customer status is not enabled, please submit the customer in time Line approval.
                ');
             }else if (' Pending approval '. Equals (A.customer_status__c)}} {        Salesorder.adderror (' Customer status is pending, please wait for approval to complete before you create the customer's order.

                '); }else{if (contactlist.size () >0) {if (String.isnotblank (contactlist[0). FirstName)) {Salesorder.consignee__c = contactlist[0]. Firstname+contactlist[0].
                        LastName; }else{Salesorder.consignee__c = contactlist[0].
                        LastName; } Salesorder.consigneephone__c = Contactlist[0].
                    Phone_number2__c; }else{Salesorder.adderror (' customer does not have the relevant contact person, please timely maintenance.
                    '); }}}}//salesorder.technicsrequiment__c = A.technol
        Ogicalrequirements1__c;
        Salesorder.industrytypes__c = A.industry;
        Salesorder.materialtype__c = A.material_type2__c; if (salesorder.delivery__c== ' home delivery ') {Salesorder.deliveryaddress__c = A.deliveryaDdress__c;
        }else if (salesorder.delivery__c== ' self pickup ') {salesorder.deliveryaddress__c = label.companyaddress; }} if (Trigger.isupdate) {if (salesorder.statue__c== ' 1 ' &&trigger. Oldmap.get (SalesOrder. Id). statue__c== ' 0 ') {a = [SELECT id,technologicalrequirements1__c,customer_status__c, industry, Material_type
            2__c, Name, Phone__c, deliveryaddress__c from account WHERE Id =: Salesorder.customer__c]; If (' not enabled '. Equals (A.customer_status__c)) {Salesorder.adderror (' customer status is not enabled, please submit customer for approval in time.
            '); }else if (' Pending '. Equals (A.customer_status__c)) {Salesorder.adderror (' customer status is pending, please wait for approval to complete before you create the customer's order.

            ');
            } list<attachment> attachmentlist = [select id from Attachment where parentid=:salesorder.id]; list<contentversion> contentversionlist = [SELECT Id from ContentVersion where Firstpublishlocationid=:salesord
            Er.id]; List<contentdocumentlink> cdllist = [SELECT Id, Linkedentityid, Contentdocumentid from Contentdocumentlink where LinkedEntityId
            =:salesorder.id]; if (Attachmentlist.size () <=0&&contentversionlist.size () <=0&&cdllist.size () <=0) {SA Lesorder.adderror (' Sales order submitted for approval, there is no upload contract, three certificates and other information attachments, not allowed to submit approval.
            '); }}}//cross-region alert if (trigger.isafter&&salesorder.statue__c== ' 1 ') {list<customobject5__ C> Userconfig=[select Id,office__c,branch__c,business_area__c,productline__c,user__c,user__r.name,position__c From Customobject5__c WHERE User__c=:salesorder.
        ownerID and Productline__c!=null]; if (Userconfig.size () >0&&salesorder.deliveryplace__c!=null) {set<id> UIds = new SET&LT;ID&G
            t; (); String msg = ' salesman ' + userconfig[0]. User__r.name + ' There are cross-regional sales, please be aware.
            ';
            set<string> uline = new set<string> ();
            Boolean isfp=true; String Salearea = SalEsorder.salearea__c.substring (3,7);//Sales area Region__c reg = [SELECT Name from Region__c WHERE id=:salesorder.de
            Liveryplace__c]; for (Customobject5__c c5:userconfig) {if (!c5.productline__c.contains (Salearea)) {////If the available product line does not include a sales area, it is sold across product lines (not required
                to chatter) ISFP = false; if (c5.position__c== ' branch manager ' | | c5.position__c== ' Domestic trade Director ' | |
                c5.position__c== ' deputy manager of Big account ') {ISFP = false; }else{//Other positions the person across the region chatter to the upper set<string> Bareas = new Set<string> (c5 .
                    Business_area__c.split (';')); for (String s:bareas) {if (reg.  
                    Name.contains (s)) {ISFP = false;//If the address contains a salesperson's sales area is not cross-region (not chatter)} }}} if (ISFP) {List<customobje ct5__c> upper = new list<customobject5__c> (); if (userconfig[0].branch__c== ' Precision/Smart Device branch ') {upper=[select Id,branch__c,user__c,user__r.name,position__c 
                From Customobject5__c WHERE branch__c=:userconfig[0].branch__c and position__c= ' branch manager ' and Productline__c=:salearea]; }else{Upper=[select Id,branch__c,user__c,user__r.name,position__c from Customobject5__c 
                WHERE Branch__c=:userconfig[0].branch__c and position__c= ' branch manager ']; } if (Upper.size () >0) {chattersender.send (upper[0]. User__c,msg,salesorder.
                ID); }    
            }    
        } 
    }   
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.