• Basil Robertson
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hi everyone,

I am trying to copy across the name of a custom child master relatioship object to a field on the opportunity, this is the code I currently have and the logs are saying its running succesfully but it isn't making any changes to the field in the opportunity:
 
trigger PullContactNum on Contract2__c (after insert, after update) {
    set<ID> opportunityids = new set<ID>();
    for(integer i=0; i < Trigger.size; i++){
	Contract2__c newContract = Trigger.new[i];
	if(Trigger.isInsert && newContract.Name != null)
	{opportunityids.add(newContract.Opportunity__c);}
    }
        



	if(opportunityids.isEmpty()){
		return;		
	}


List<Contract2__c> allContracts = [SELECT id, Name, Opportunity__c FROM Contract2__c WHERE Opportunity__c IN :opportunityids];
Map<Id, List<Contract2__C>> oppToContractMap = new Map <id,list<Contract2__c>>();
    for(Contract2__c contractCon : allContracts){
        list<Contract2__c> oppContract = oppToContractMap.get(contractCon.Opportunity__c);
        if(oppContract == null){
            oppContract = new List<Contract2__c>();
            oppToContractMap.put(contractCon.Opportunity__c,oppContract);
        }
        oppContract.add(contractCon);
    }    

Map<Id,Opportunity> conMap = new Map<Id,Opportunity>([SELECT id FROM Opportunity WHERE id IN :opportunityids]);
    for (Id conID : conMap.keySet()){
        List<Contract2__c> contracts = oppToContractMap.get(conId);
        String JobNum = '';
        if(contracts != null){
            for(Contract2__c contract: contracts){
            	JobNum += contract.Name;
                JobNum += ', ';
        	}
        }
        conMap.get(conID).Contract_Name__c = JobNum;
    }

	update conMap.values();

}

Thanks!

Hi Everyone,

I have custom object, lets say contract (API name contract2__c), which is linked to opportuinities via a lookup relationship. I would like a field update on a field on the opportunity "contract_name__c" with the name of the contract which is linked to the opportunity.

This is the code I have so far:
 

trigger PullContactName on Opportunity (before insert, before update) {
     set<ID>cObjectID = new set<ID>();
    
    for (Opportunity opp : Trigger.new){
        if(opp.contract2__c != null){
            cObjectID.add(opp.contract2__c);
        }
    }
    
    if(!cObjectID.isEmpty()){
        
        Map<ID,contract2__c> cObjectMap = new Map<ID,contract2__c>([select Id,Name from contract2__c where Id IN: cObjectID])
;

        for(Opportunity opp : trigger.new){
            if(cObjectMap.get(opp.contract2__c).Name != null){
                opp.Contract_Name__c = cObjectMap.get(opp.contract2__c).Name;
            }
        }
        
    }
}

But it keeps throwing a problem stating on line 6 that "Invalid field contract2__c for SObject Opportunity"
Please help

kind regards,
Basil Robertson
Hi everyone,

I am trying to copy across the name of a custom child master relatioship object to a field on the opportunity, this is the code I currently have and the logs are saying its running succesfully but it isn't making any changes to the field in the opportunity:
 
trigger PullContactNum on Contract2__c (after insert, after update) {
    set<ID> opportunityids = new set<ID>();
    for(integer i=0; i < Trigger.size; i++){
	Contract2__c newContract = Trigger.new[i];
	if(Trigger.isInsert && newContract.Name != null)
	{opportunityids.add(newContract.Opportunity__c);}
    }
        



	if(opportunityids.isEmpty()){
		return;		
	}


List<Contract2__c> allContracts = [SELECT id, Name, Opportunity__c FROM Contract2__c WHERE Opportunity__c IN :opportunityids];
Map<Id, List<Contract2__C>> oppToContractMap = new Map <id,list<Contract2__c>>();
    for(Contract2__c contractCon : allContracts){
        list<Contract2__c> oppContract = oppToContractMap.get(contractCon.Opportunity__c);
        if(oppContract == null){
            oppContract = new List<Contract2__c>();
            oppToContractMap.put(contractCon.Opportunity__c,oppContract);
        }
        oppContract.add(contractCon);
    }    

Map<Id,Opportunity> conMap = new Map<Id,Opportunity>([SELECT id FROM Opportunity WHERE id IN :opportunityids]);
    for (Id conID : conMap.keySet()){
        List<Contract2__c> contracts = oppToContractMap.get(conId);
        String JobNum = '';
        if(contracts != null){
            for(Contract2__c contract: contracts){
            	JobNum += contract.Name;
                JobNum += ', ';
        	}
        }
        conMap.get(conID).Contract_Name__c = JobNum;
    }

	update conMap.values();

}

Thanks!