You need to sign in to do that
Don't have an account?
Basil 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:
Thanks!
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
"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.
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)
Update the Contract2__r name from the object details.