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
vivek singh 26vivek singh 26 

Hi sir, when we make a trigger on opportunity & fill all field then only one contact role made but when we fill contact filled and opportunity then contact role make 2 value how i solve it

Example ----When we fullfill contact then opporetunity then two contact role

Action            Contact  Name        Company Name           Email                                                        Phone                           Role                       Primary
Edit |  Del     Mark Sze                  informgroup.com            mark.sze@informgroup.com                                                                                    Checked
Edit | Del       Mark Sze                 informgroup.com            mark.sze@informgroup.com                                                      Buyer                         Checked

_______________________________________________________________________________________
When we fill oportunity then only one contact role ___
Action            Contact  Name        Company Name              Email                                                        Phone                           Role                        Primary
Edit |  Del     Mark Sze                  informgroup.com            mark.sze@informgroup.com                                                                                         Checked

How i solve it and i want to only one contact role in under opportunity







Trigger-------------
________________________
trigger OpportunityTrigger on Opportunity (After insert)
{
    if(trigger.isAfter && trigger.isInsert)
    { system.debug('After insert');
        opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
        objopportunityTriggerHandler.createNewContact(trigger.new);
    }
}

________________________________
Class is _______________________________
Public class opportunityTriggerHandler
{
   
    public void createNewContact(List<Opportunity> triggerNew)
    {
        List<OpportunityContactRole> lstOppContactRole = new list<OpportunityContactRole>();
        if(triggerNew != null && triggerNew.size() > 0)
        {
            for(Opportunity objOpp : triggerNew)
            {
                if(objOpp.Buyer__c != null)
                {
                    OpportunityContactRole objOppContactRole = new OpportunityContactRole();
                   
                    objOppContactRole.ContactId = objOpp.Buyer__c;
                    objOppContactRole.OpportunityId = objOpp.ID;
                    objOppContactRole.Role = 'Buyer';
                   
                    lstOppContactRole.add(objOppContactRole );
                }
            }
        }
       
        if(lstOppContactRole.size() > 0)
      
       insert lstOppContactRole;
       
    }
}




Best Answer chosen by vivek singh 26
Sfdc CloudSfdc Cloud
HI Vivek
As i assume that buyer__c is lookup to contact object and created trigger in my org .Its working fine for me
trigger Check on Opportunity (After insert)
{
      List<OpportunityContactRole> lstOppContactRole = new list<OpportunityContactRole>();
	   for(Opportunity objOpp : trigger.New)
            {
                if(objOpp.Buyer__c != null)
                {
                    OpportunityContactRole objOppContactRole = new OpportunityContactRole();
                   
                    objOppContactRole.ContactId = objOpp.Buyer__c;
                    objOppContactRole.OpportunityId = objOpp.ID;
                    objOppContactRole.Role = 'Buyer';
                   
                    lstOppContactRole.add(objOppContactRole );
                }
            }
     if(lstOppContactRole.size() > 0)
       insert lstOppContactRole;
}
Try This code..
If this answer helps you,Please mark it as best answer to help others :)
Thanks

All Answers

Ramu_SFDCRamu_SFDC
Can you please describe your issue again with screen shots step wise so that either me or someone on the board can provide you their insights.
Sfdc CloudSfdc Cloud
HI Vivek
As i assume that buyer__c is lookup to contact object and created trigger in my org .Its working fine for me
trigger Check on Opportunity (After insert)
{
      List<OpportunityContactRole> lstOppContactRole = new list<OpportunityContactRole>();
	   for(Opportunity objOpp : trigger.New)
            {
                if(objOpp.Buyer__c != null)
                {
                    OpportunityContactRole objOppContactRole = new OpportunityContactRole();
                   
                    objOppContactRole.ContactId = objOpp.Buyer__c;
                    objOppContactRole.OpportunityId = objOpp.ID;
                    objOppContactRole.Role = 'Buyer';
                   
                    lstOppContactRole.add(objOppContactRole );
                }
            }
     if(lstOppContactRole.size() > 0)
       insert lstOppContactRole;
}
Try This code..
If this answer helps you,Please mark it as best answer to help others :)
Thanks
This was selected as the best answer
vivek singh 26vivek singh 26
HI Sir,
my problem is i want only one contact role in under opportunity when we full fill all field then only on contact role but when we make contact then opportunity then make 2 contact role how i solve it



Action            Contact  Name        Company Name           Email                                                        Phone                           Role                       Primary
Edit |  Del     Mark Sze                  informgroup.com            mark.sze@informgroup.com                                                                                    Checked
Edit | Del       Mark Sze                 informgroup.com            mark.sze@informgroup.com                                                      Buyer                         Checked

_______________________________________________________________________________________
When we fill oportunity then only one contact role ___
Action            Contact  Name        Company Name              Email                                                        Phone                           Role                        Primary
Edit |  Del     Mark Sze                  informgroup.com            mark.sze@informgroup.com                                                                                         Checked