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
Basil RobertsonBasil Robertson 

Opportunity Field Update from child master relationship child

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!
Himanshu MaheshwariHimanshu Maheshwari
You can use the below code :
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;		
	}

	for(Opportunity opp : [SELECT id, Name, Contract_Name__c, (SELECT id, Name, Opportunity__c FROM Contract2__r) FROM Contract2__c WHERE Opportunity__c IN :opportunityids]){
		String JobNum = '';
		for(Contract2__c con : Contract2__r){
			JobNum += contract.Name;
		    JobNum += ', ';
		}
		opp.Contract_Name__c = JobNum;

		oppList.add(opp);
	}

	if(oppList.size() > 0)
		update oppList;
}
Thanks.
Himanshu
 
Basil RobertsonBasil Robertson
Thanks for the speedy reply, I've tried that but firstly I don't have a field called "Contract_Name__c" it is just "Name". But then it throws the following error when refering to: "SELECT id, Name, Opportunity__c FROM Contract2__r"

"Didn't understand relationship 'Contract2__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names."

Please advise.
Himanshu MaheshwariHimanshu Maheshwari
Contract2__r is the Child Relationship Name which you can see in your look up fields details.
For details regarding Child Relationship Name use the link :  https://developer.salesforce.com/docs/atlas.en-us.200.0.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_custom_objects.htm#sforce_api_calls_soql_relationships_and_custom_objects (https://developer.salesforce.com/docs/atlas.en-us.200.0.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_custom_objects.htm#sforce_api_calls_soql_relationships_and_custom_objects)
for(Opportunity opp : [SELECT id, Name, Contract_Name__c, (SELECT id, Name, Opportunity__c FROM Contract2__r) FROM Opportunity WHERE Id IN :opportunityids]){
	String JobNum = '';
	for(Contract2__c con : Contract2__r){
		JobNum += contract.Name;
	       JobNum += ', ';
	}
	opp.Contract_Name__c = JobNum;
	oppList.add(opp);
}

Update the Contract2__r name from the object details.