+ Start a Discussion
Manish Anand 10Manish Anand 10 

Trigger:-Error while updating related record

Hi There,

I am writing a trigger to update a opporutnity related record, if any new /updated account doesn't have a one. 

trigger AddRelatedRecord on Account (after insert,after update) {
    List<Opportunity> oppList = new List<Opportunity>();
    
    // Get the related opportunities for the accounts in this trigger
    Map<Id,Account> acctsWithOpps = new Map<Id,Account>(
        [SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);
    
    // Add an opportunity for each account if it doesn't already have one.
    // Iterate through each account.
    for(Account a : Trigger.New) {
        System.debug('acctsWithOpps.get(a.Id).Opportunities.size()=' + acctsWithOpps.get(a.Id).Opportunities.size());
        // Check if the account already has a related opportunity.
        if (acctsWithOpps.get(a.Id).Opportunities.size() == 0) {
            // If it doesn't, add a default opportunity
            oppList.add(Name=a.Account + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),AccountId=a.Id);
        }           
    }

    if (oppList.size() > 0) {
        insert oppList;
    }

}

It throws an error in line 15: Variable doesn't exist:Name.

What I am missing here?
Vijay NagarathinamVijay Nagarathinam
HI Manish,

Use the below statement,

  for(Account a : Trigger.New) {
        System.debug('acctsWithOpps.get(a.Id).Opportunities.size()=' + acctsWithOpps.get(a.Id).Opportunities.size());
        // Check if the account already has a related opportunity.
        if (acctsWithOpps.get(a.Id).Opportunities.size() == 0) {
            // If it doesn't, add a default opportunity
            oppList.add(Name=a.name + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),AccountId=a.Id);
        }           
    }

Let me know if you need any help regarding this.

Thanks,
Vijay
Vijay NagarathinamVijay Nagarathinam
Hi Manish,

Use the below code,
 
trigger AddRelatedRecord on Account (after insert,after update) {
    List<Opportunity> oppList = new List<Opportunity>();
    Map<Id,Account> acctsWithOpps = new Map<Id,Account>();
	for(Account newAccount : Trigger.new){
		acctsWithOpps.put(newAccount.Id,newAccount);
	}
	List<Opportunity> oppList = [SELECT Id,AccountId FROM Opportunity WHERE Id IN : acctsWithOpps.keyset()];
	if(oppList.size() == 0){
		for(Account acc : Trigger.new){
			oppList.add(new Opportunity(name = acc.name, stagename = 'Prospecting', CloseDate = System.today().addMonths(1), AccountId=acc.Id));
		}
	}
    if (oppList.size() > 0) {
        insert oppList;
    }
}

Let me know if you need any help regarding this

Thanks,
Vijay
Manish Anand 10Manish Anand 10
Hi Vijay,

The first query gives the same error-Variable doesn't exist:Name
The second query gives error-Invalid Constructor Syntax,name=Value pairs can only be used for Sobjects.
Mahesh DMahesh D
Hi Manish,

Please take the below code which covers all scenarios and bulkified trigger.

Also added the comments so that it will be easy for you to understand.
 
trigger AccountAfterTrigger on Account (after insert, after update) {
    Map<Id, Opportunity> accOppMap = new Map<Id, Opportunity>();
	List<Opportunity> finalOppList = new List<Opportunity>();
	
	//
	// We need to check for the existing Opportunities only for update scenario as there will not be any Opportunities
	// for newly created Accounts.
	//
	if(Trigger.isUpdate) {
		for(Opportunity opp: [Select Id, Name, AccountId from Opportunity where AccountId IN: Trigger.newMap.keySet()]) {
			accOppMap.put(opp.AccountId, opp);
		}
	}
	
	//Iterating through all the input trigger.
	for(Account acc: Trigger.new) {
		// Check if it is insert - Create Opportunity for every Account.
		// Check if it is update - Check if the Opportunity exists or not, if it not then only create new opportunity.
		if(Trigger.isInsert || (Trigger.isUpdate && accOppMap.get(acc.Id)) == null)) {
			Opportunity opp = new Opportunity();
			opp.Name = acc.Name+' Opportunity';
			opp.StageName = 'Prospecting';
			opp.CloseDate = System.today().addMonths(1);
			opp.AccountId = acc.Id;
			finalOppList.add(opp);
		}
	}
	
	if(!finalOppList.isEmpty()) {
		insert finalOppList;
	}
}

Regards,
Mahesh
Mahesh DMahesh D
Hi Manish,

I also tested the above code in my DE environment and everything is working properly.

Regards,
Mahesh
Manish Anand 10Manish Anand 10
Hi Mahesh,

Above code throws an error-Variable Doesn't exist:AccountID, in line11.

Regards,
Manish.
Mahesh DMahesh D
Hi Manish,

I tested it im org and it is working fine.

Please check if if you org is enabled Person Accounts?

Regards,
Mahesh
Manish Anand 10Manish Anand 10
Hi Mahesh,

Could you please check one of my VF and Apex question in the below link?
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DBaZIAW
This is unsolved, so far. It would be great, if you could, please provide some of tips/hints/solution for this.

Regards,
Manish.
 
Mahesh DMahesh D
Hi Manish,

Please paste your latest code here.

I would like to see what was the error, because as I tested in my environment why it didn't work for you.

Regards,
Mahesh