You need to sign in to do that
Don't have an account?

Update related Account Account fields from a Custom Object
Hi everyone,
I'm a new developer so please bear that in mind! I'm trying to update an Account's custom fields from a Custom Object called Pub_Opportunity__c. The account has a lookup relationship to the pub_opportunity__c. But I only want the Account to update once with these fields as sometimes there are many pub opportunities related to one Account. I just want to retain the original values.
When I use the code below, it keeps updating the Account every single time with the new Pub Opportunity's fields regardless of the condition. For example, when I change the Account_Status__c field on the Account to 'Active', when I create a new Pub Opportunity, it still updates the Account_Status__c to 'Closed Won - Implementing'. Even when the Link_to_Contract__c field on the Account is filled out, it still updates the field with the new value from the Pub Opportunity.
Would appreciate your help!
public static void updatePubs(Map<Id,Pub_Opportunity__c> pubOppMap){
List<Account> aList = new List<Account>();
for(Pub_Opportunity__c p : pubOppMap.values()){
Account a = new Account();
if(a.Account_Status__c != 'Active'){
a.Account_Status__c = 'Closed Won - Implementing';
a.id = p.Publisher__c;
if(String.Isblank(a.Link_to_Contract__c)){
a.Link_To_Contract__c = p.Link_to_Contract__c;
}
aList.add(a);
}
if(aList.size() > 0){
update alist;
}
}
}
I'm a new developer so please bear that in mind! I'm trying to update an Account's custom fields from a Custom Object called Pub_Opportunity__c. The account has a lookup relationship to the pub_opportunity__c. But I only want the Account to update once with these fields as sometimes there are many pub opportunities related to one Account. I just want to retain the original values.
When I use the code below, it keeps updating the Account every single time with the new Pub Opportunity's fields regardless of the condition. For example, when I change the Account_Status__c field on the Account to 'Active', when I create a new Pub Opportunity, it still updates the Account_Status__c to 'Closed Won - Implementing'. Even when the Link_to_Contract__c field on the Account is filled out, it still updates the field with the new value from the Pub Opportunity.
Would appreciate your help!
public static void updatePubs(Map<Id,Pub_Opportunity__c> pubOppMap){
List<Account> aList = new List<Account>();
for(Pub_Opportunity__c p : pubOppMap.values()){
Account a = new Account();
if(a.Account_Status__c != 'Active'){
a.Account_Status__c = 'Closed Won - Implementing';
a.id = p.Publisher__c;
if(String.Isblank(a.Link_to_Contract__c)){
a.Link_To_Contract__c = p.Link_to_Contract__c;
}
aList.add(a);
}
if(aList.size() > 0){
update alist;
}
}
}
Map<id,Account> accMap = new map<id,account>([slect id,Account_Status__c,Link_to_Contract__c from account where id in : accountIds]);
All Answers
Try This:
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/
Salesforce latest interview questions :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
Map<Id,Account> accMap = new map<Id,Account> [SELECT Id, Account_Status__c, Link_to_Contract__c from Account where Id in : accountIds];
The problems are:
1) Unexpected token '[' .
2) Expression cannot be a statement.
I did change the word to Select from the original code as I assumed there was a spelling error.
Map<id,Account> accMap = new map<id,account>([slect id,Account_Status__c,Link_to_Contract__c from account where id in : accountIds]);