+ Start a Discussion
krish99krish99 

How to update the field when mail sent case

HI,

      I have a object quote, for that object custom button is there to send an email, when user sends an email from salesforce that details all are storing in activity history,for that quote i have a status field(picklist) intinally while creating quote the status is request, when email sends means that status field have to chagne automatically to proposal send. how can i achieve this.. i can't getting exactly.
ShashForceShashForce
Hi Krish,

The email will be saved as a Task record under the custom object. You can write a trigger on Task object to achieve this.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
krish99krish99
@Shashank_SFDC,

   can you give me some sample trigger for above task.
ShashForceShashForce
Here's a sample:

trigger updatequotestatus on task (after insert, after update){
	set<Id> quoteIds = new set<Id>();
	list<quote__c> quotes = new list<quote__c>();
	for(task t:trigger.new){
		if((t.what.getsobjecttype()==quote__c.sobjecttype)&&(t.subject.substring(0,5)=='Email'){
			quoteIds.add(t.whatId);
		}
	}
	for(quote__c q:[select status__c from quote__c where Id IN quoteIds]){
		quote__c qt = new quote__c();
		qt.Id = q.Id;
		qt.status__c = 'Proposal Sent';
		quotes.add(qt);
	}
	if(quotes.size()>0){
		update quotes;
	}
}

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
krish99krish99
@Shashank,

                      whille sendong an email i am getting thr following error what can i do any suggestions

     There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger updatequotestatus caused an unexpected exception, contact your administrator: updatequotestatus: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updatequotestatus: line 5, column 1".

   Due to this error i am unable to send the email...
ShashForceShashForce
Hi,

please try this:

trigger updatequotestatus on task (after insert, after update){
	set<Id> quoteIds = new set<Id>();
	list<quote__c> quotes = new list<quote__c>();
	for(task t:trigger.new){
		if(t.whatId<>null){
			if((t.what.getsobjecttype()==quote__c.sobjecttype)&&(t.subject.substring(0,5)=='Email'){
				quoteIds.add(t.whatId);
			}
		}
	}
	for(quote__c q:[select status__c from quote__c where Id IN quoteIds]){
		quote__c qt = new quote__c();
		qt.Id = q.Id;
		qt.status__c = 'Proposal Sent';
		quotes.add(qt);
	}
	if(quotes.size()>0){
		update quotes;
	}
}


krish99krish99
Shashank_SFDC

   Again sa,e error the code which i am using

trigger updatequotestatus on task (after insert, after update){
    set<Id> quoteIds = new set<Id>();
    list<quote__c> quotes = new list<quote__c>();
      for(task t:trigger.new){
  
       if(t.whatId!=null){

        if((t.what.getsobjecttype()==quote__c.sobjecttype)&&(t.subject.substring(0,5)=='Email')){
            quoteIds.add(t.whatId);
        }
      }
    }
    for(quote__c q:[select StageName__c from quote__c where Id IN : quoteIds]){
        quote__c qt = new quote__c();
        qt.Id = q.Id;
        qt.StageName__c = 'Proposal Sent';
        quotes.add(qt);
    }
    if(quotes.size()>0){
        update quotes;
    }
}

Arjun bura SFDCArjun bura SFDC
trigger updatequotestatus on task (after insert, after update){
    set<Id> quoteIds = new set<Id>();
    list<quote__c> quotes = new list<quote__c>();
      for(task t:trigger.new){
  
       if(t.whatId!=null){

        if((t.whatId.getsobjecttype()==quote__c.sobjecttype)&&(t.subject.substring(0,5)=='Email')){
            quoteIds.add(t.whatId);
        }
      }
    }
    for(quote__c q:[select StageName__c from quote__c where Id IN : quoteIds]){
        quote__c qt = new quote__c();
        qt.Id = q.Id;
        qt.StageName__c = 'Proposal Sent';
        quotes.add(qt);
    }
    if(quotes.size()>0){
        update quotes;
    }
}

t.what.getsobjecttype()==quote__c.sobjecttype here replace what with whatId
Arjun bura SFDCArjun bura SFDC
because your getting "what" as null,