function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
S Balaji 8S Balaji 8 

Can anyone share me the trigger code for the below scenario please?

When an Order is loaded into CRM, using the associated Account, find the child Contact using the “RDOC(field on Contact)” field as a flag, populate this Contact in “DR” Lookup field on Order 
Best Answer chosen by S Balaji 8
AnkaiahAnkaiah (Salesforce Developers) 
Hi Balaji,

try with below code and modify the field API names as per your org.
trigger updatecontact on order(before insert,before update){

set<id> accids = new set<id>();

for(order or:trigger.new){

if(or.Account__c!=Null){
accids.add(or.Account__c);
}
}

map<id,id> mapaccwithcontactids = new map<id,id>();

for(contact con:[select id,Accountid,RDOC__c from contact where Accountid IN:accids AND RDOC__c=TRUE]){

mapaccwithcontactids.add(con.accountid,con.id);
}

for(order or:trigger.new){

if(mapaccwithcontactids.containskey(or.account__c)){
or.DR__c = mapaccwithcontactids.get(or.account__c);

}
}

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Balaji,

RDOC field is available in account also?

THanks!!
S Balaji 8S Balaji 8
Hi Ankiah,

Nopes, it's only on Contact object.

Best Regards,
Balaji

 
AnkaiahAnkaiah (Salesforce Developers) 
order is created with account then automatically link the contact to the order object. Am i correct?

Thanks!!
S Balaji 8S Balaji 8
Yes that’s right. But based on RDOC field as well , if it’s checked then only link that contact to the order object. Regards , Balaji
AnkaiahAnkaiah (Salesforce Developers) 
Hi Balaji,

try with below code and modify the field API names as per your org.
trigger updatecontact on order(before insert,before update){

set<id> accids = new set<id>();

for(order or:trigger.new){

if(or.Account__c!=Null){
accids.add(or.Account__c);
}
}

map<id,id> mapaccwithcontactids = new map<id,id>();

for(contact con:[select id,Accountid,RDOC__c from contact where Accountid IN:accids AND RDOC__c=TRUE]){

mapaccwithcontactids.add(con.accountid,con.id);
}

for(order or:trigger.new){

if(mapaccwithcontactids.containskey(or.account__c)){
or.DR__c = mapaccwithcontactids.get(or.account__c);

}
}

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
 
This was selected as the best answer
S Balaji 8S Balaji 8
Thank you so much for the help here, this works like a gem.
If you don't mind, will you be able to share me the test class as well please , it will be very helpful.
AnkaiahAnkaiah (Salesforce Developers) 
Thanks for the update.
Would you be able to post this as a new question so I can have a track of my efforts. Appreciate your understanding

Thanks!!
 
S Balaji 8S Balaji 8
Sure. Same has been done.Kindly help.Please refer the below URL. https://developer.salesforce.com/forums/ForumsMain?id=9062I000000R1qdQAC Regards, Balaji
S Balaji 8S Balaji 8
Hi Ankiah,

One query please, in my org for one account we have multiple contacts, in this scenario how does this work please?
If more than one contact have the RDOC field checked, then which contact will be considered?

Regards,
Balaji
S Balaji 8S Balaji 8
Hi Ankiah,

Can you please help me on the below scenario if possible.

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000R1u6QAC

Regards,
Balaji